package com.gqz.defect.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.poi.ExcelUtil;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.defect.entity.EnStockEquipDefectEntity;
import com.gqz.defect.enumEntity.CodeEnum;
import com.gqz.defect.mapper.EnStockEquipDefectMapper;
import com.gqz.defect.mapstruct.EnStockEquipDefectMapstruct;
import com.gqz.defect.service.IEnStockEquipDefectService;
import com.gqz.defect.vo.AuditDefectsVo;
import com.gqz.defect.vo.EnDefectsTjVo;
import com.gqz.defect.vo.EnStockEquipDefectVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 能源-盘点任务设备缺陷表 业务逻辑实现层ServiceImpl
 */
@Service
@Slf4j
public class EnStockEquipDefectServiceImpl extends ServiceImpl<EnStockEquipDefectMapper, EnStockEquipDefectEntity> implements IEnStockEquipDefectService {

    @Autowired
    private EnStockEquipDefectMapstruct mapstruct;

    @Override
    public List<EnStockEquipDefectVo> getById(String id) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(EnStockEquipDefectEntity::getId, id);
        queryWrapper.selectAll(EnStockEquipDefectEntity.class);
        List<EnStockEquipDefectEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnStockEquipDefectVo> page(RequestForm<EnStockEquipDefectVo> requestForm) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(EnStockEquipDefectEntity.class);
        /*queryWrapper.select(EnWzJfEquipEntity::getCcbm);
        queryWrapper.innerJoin(EnWzJfEquipEntity.class,EnWzJfEquipEntity::getSbbm,EnStockEquipDefectEntity::getEquipNo);*/
        queryWrapper.eq(EnStockEquipDefectEntity::getIsDel, 0);
        //queryWrapper.eq(EnWzJfEquipEntity::getSfsc, 0);
        queryWrapper.notIn(EnStockEquipDefectEntity::getDefectStatusCode, 0, 1, 3);
        queryWrapper.orderByDesc(EnStockEquipDefectEntity::getCreatTime);
        if (requestForm.getEntity().getUnitNo() != null && !"".equals(requestForm.getEntity().getUnitNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getUnitNo, requestForm.getEntity().getUnitNo());
        }
        if (requestForm.getEntity().getTaskName() != null && !"".equals(requestForm.getEntity().getTaskName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getTaskName, requestForm.getEntity().getTaskName());
        }
        if (requestForm.getEntity().getDefectName() != null && !"".equals(requestForm.getEntity().getDefectName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getDefectName, requestForm.getEntity().getDefectName());
        }
        if (requestForm.getEntity().getTeamName() != null && !"".equals(requestForm.getEntity().getTeamName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getTeamName, requestForm.getEntity().getTeamName());
        }
        if (requestForm.getEntity().getKfName() != null && !"".equals(requestForm.getEntity().getKfName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getKfName, requestForm.getEntity().getKfName());
        }
        if (requestForm.getEntity().getJfName() != null && !"".equals(requestForm.getEntity().getJfName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getJfName, requestForm.getEntity().getJfName());
        }
        if (requestForm.getEntity().getEquipName() != null && !"".equals(requestForm.getEntity().getEquipName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getEquipName, requestForm.getEntity().getEquipName());
        }
        if (requestForm.getEntity().getCreator() != null && !"".equals(requestForm.getEntity().getCreator())) {
            queryWrapper.like(EnStockEquipDefectEntity::getCreator, requestForm.getEntity().getCreator());
        }
        if (requestForm.getEntity().getJfNo() != null && !"".equals(requestForm.getEntity().getJfNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getJfNo, requestForm.getEntity().getJfNo());
        }
        if (requestForm.getEntity().getTaskNo() != null && !"".equals(requestForm.getEntity().getTaskNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getTaskNo, requestForm.getEntity().getTaskNo());
        }
        if (requestForm.getEntity().getEquipDefectModel() != null && !"".equals(requestForm.getEntity().getEquipDefectModel())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getEquipDefectModel, requestForm.getEntity().getEquipDefectModel());
        }
        if (requestForm.getEntity().getDangerTypeNo() != null && !"".equals(requestForm.getEntity().getDangerTypeNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getDangerTypeNo, requestForm.getEntity().getDangerTypeNo());
        }
        if (requestForm.getEntity().getMaterialTypeNo() != null && !"".equals(requestForm.getEntity().getMaterialTypeNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getMaterialTypeNo, requestForm.getEntity().getMaterialTypeNo());
        }
        if (requestForm.getEntity().getTaskSourceNo() != null && !"".equals(requestForm.getEntity().getTaskSourceNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getTaskSourceNo, requestForm.getEntity().getTaskSourceNo());
        }
        if (requestForm.getEntity().getEquipDefectType() != null && !"".equals(requestForm.getEntity().getEquipDefectType())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getEquipDefectType, requestForm.getEntity().getEquipDefectType());
        }
        if (requestForm.getEntity().getDefectStatusCode() != null && !"".equals(requestForm.getEntity().getDefectStatusCode())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getDefectStatusCode, requestForm.getEntity().getDefectStatusCode());
        }
        if (requestForm.getEntity().getCreatTime() != null && !"".equals(requestForm.getEntity().getCreatTime())) {
            queryWrapper.gt(EnStockEquipDefectEntity::getCreatTime, requestForm.getEntity().getCreatTime());
        }
        if (requestForm.getEntity().getCreatTime_to() != null && !"".equals(requestForm.getEntity().getCreatTime_to())) {
            queryWrapper.le(EnStockEquipDefectEntity::getCreatTime, requestForm.getEntity().getCreatTime_to());
        }

        Page<EnStockEquipDefectEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        IPage<EnStockEquipDefectEntity> iPage = baseMapper.selectJoinPage(page, EnStockEquipDefectEntity.class, queryWrapper);
        List<EnStockEquipDefectEntity> retestVoList = iPage.getRecords().stream().map(item -> {
            item.setEquipDefectTypeName(CodeEnum.getValue(item.getEquipDefectType()));
            if ("2".equals(item.getDefectStatusCode())) {
                item.setDefectStatusName("未处理");
            }
            return item;
        }).collect(Collectors.toList());
        page.setRecords(retestVoList);
        //page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnStockEquipDefectVo> requestForm) {
        EnStockEquipDefectEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(EnStockEquipDefectEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnStockEquipDefectVo> forms) {
        List<EnStockEquipDefectEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockEquipDefectEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnStockEquipDefectVo> forms) {
        List<EnStockEquipDefectEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockEquipDefectEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @SneakyThrows
    @Override
    public void exportExcel(HttpServletResponse response, RequestForm<EnStockEquipDefectVo> requestForm) {
        //设置信息头，告诉浏览器内容为excel类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //文件名称
        String fileName = "能源-盘点任务设备缺陷表信息�?.xls";
        //sheet名称
        String sheetName = "能源-盘点任务设备缺陷表列表";
        fileName = new String(fileName.getBytes(), "ISO-8859-1");
        //设置下载名称
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        if (requestForm.getPageSize() < 1000L) {
            requestForm.setPageSize(1000L);
        }
        //获取到业务数�?
        Page<EnStockEquipDefectVo> page = this.page(requestForm);
        new ExcelUtil<>(EnStockEquipDefectVo.class).exportExcel(response, page.getRecords(), sheetName);
    }

    /**
     * 审核
     *
     * @param auditDefectsVo
     * @return
     */
    @Override
    public Boolean auditDefects(AuditDefectsVo auditDefectsVo) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .eq(StringUtils.isNotBlank(auditDefectsVo.getId()), EnStockEquipDefectEntity::getId, auditDefectsVo.getId())
                .eq(StringUtils.isNotBlank(auditDefectsVo.getTaskNo()), EnStockEquipDefectEntity::getTaskNo, auditDefectsVo.getTaskNo())
                .eq(EnStockEquipDefectEntity::getIsDel, "0");
        EnStockEquipDefectEntity temp = new EnStockEquipDefectEntity();
        try {
            BeanUtils.copyProperties(temp, auditDefectsVo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return update(temp, wrapper);
    }

    @Override
    public Page<EnStockEquipDefectVo> pageByState(RequestForm<EnStockEquipDefectVo> requestForm) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(EnStockEquipDefectEntity.class);
        /*queryWrapper.select(EnWzJfEquipEntity::getCcbm);
        queryWrapper.leftJoin(EnWzJfEquipEntity.class,EnWzJfEquipEntity::getSbbm,EnStockEquipDefectEntity::getEquipNo);*/
        queryWrapper.eq(EnStockEquipDefectEntity::getIsDel, 0);
        //queryWrapper.eq(EnWzJfEquipEntity::getSfsc, 0);
        queryWrapper.orderByDesc(EnStockEquipDefectEntity::getCreatTime);
        if (requestForm.getEntity().getUnitNo() != null && !"".equals(requestForm.getEntity().getUnitNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getUnitNo, requestForm.getEntity().getUnitNo());
        }
        if (requestForm.getEntity().getTaskName() != null && !"".equals(requestForm.getEntity().getTaskName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getTaskName, requestForm.getEntity().getTaskName());
        }
        if (requestForm.getEntity().getDefectName() != null && !"".equals(requestForm.getEntity().getDefectName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getDefectName, requestForm.getEntity().getDefectName());
        }
        if (requestForm.getEntity().getTeamName() != null && !"".equals(requestForm.getEntity().getTeamName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getTeamName, requestForm.getEntity().getTeamName());
        }
        if (requestForm.getEntity().getKfName() != null && !"".equals(requestForm.getEntity().getKfName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getKfName, requestForm.getEntity().getKfName());
        }
        if (requestForm.getEntity().getJfName() != null && !"".equals(requestForm.getEntity().getJfName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getJfName, requestForm.getEntity().getJfName());
        }
        if (requestForm.getEntity().getEquipName() != null && !"".equals(requestForm.getEntity().getEquipName())) {
            queryWrapper.like(EnStockEquipDefectEntity::getEquipName, requestForm.getEntity().getEquipName());
        }
        if (requestForm.getEntity().getCreator() != null && !"".equals(requestForm.getEntity().getCreator())) {
            queryWrapper.like(EnStockEquipDefectEntity::getCreator, requestForm.getEntity().getCreator());
        }
        if (requestForm.getEntity().getJfNo() != null && !"".equals(requestForm.getEntity().getJfNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getJfNo, requestForm.getEntity().getJfNo());
        }
        if (requestForm.getEntity().getKfNo() != null && !"".equals(requestForm.getEntity().getKfNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getKfNo, requestForm.getEntity().getKfNo());
        }
        if (requestForm.getEntity().getTaskNo() != null && !"".equals(requestForm.getEntity().getTaskNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getTaskNo, requestForm.getEntity().getTaskNo());
        }
        if (requestForm.getEntity().getEquipDefectModel() != null && !"".equals(requestForm.getEntity().getEquipDefectModel())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getEquipDefectModel, requestForm.getEntity().getEquipDefectModel());
        }
        if (requestForm.getEntity().getDangerTypeNo() != null && !"".equals(requestForm.getEntity().getDangerTypeNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getDangerTypeNo, requestForm.getEntity().getDangerTypeNo());
        }
        if (requestForm.getEntity().getMaterialTypeNo() != null && !"".equals(requestForm.getEntity().getMaterialTypeNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getMaterialTypeNo, requestForm.getEntity().getMaterialTypeNo());
        }
        if (requestForm.getEntity().getTaskSourceNo() != null && !"".equals(requestForm.getEntity().getTaskSourceNo())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getTaskSourceNo, requestForm.getEntity().getTaskSourceNo());
        }
        if (requestForm.getEntity().getEquipDefectType() != null && !"".equals(requestForm.getEntity().getEquipDefectType())) {
            queryWrapper.eq(EnStockEquipDefectEntity::getEquipDefectType, requestForm.getEntity().getEquipDefectType());
        }
        /*if(requestForm.getEntity().getDefectStatusCode() != null && !"".equals(requestForm.getEntity().getDefectStatusCode())){
            queryWrapper.eq(EnStockEquipDefectEntity :: getDefectStatusCode,requestForm.getEntity().getDefectStatusCode());
        }*/
        if (requestForm.getEntity().getCreatTime() != null && !"".equals(requestForm.getEntity().getCreatTime())) {
            queryWrapper.gt(EnStockEquipDefectEntity::getCreatTime, requestForm.getEntity().getCreatTime());
        }
        if (requestForm.getEntity().getCreatTime_to() != null && !"".equals(requestForm.getEntity().getCreatTime_to())) {
            queryWrapper.le(EnStockEquipDefectEntity::getCreatTime, requestForm.getEntity().getCreatTime_to());
        }
        //只查询未处理、已派单未处理的缺陷隐患
        queryWrapper.in(EnStockEquipDefectEntity::getDefectStatusCode, "2", "30");

        Page<EnStockEquipDefectEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        IPage<EnStockEquipDefectEntity> iPage = baseMapper.selectJoinPage(page, EnStockEquipDefectEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public List<EnDefectsTjVo> getDefectsStatusXx(String sdate, String edate) {
        MPJLambdaWrapper<EnStockEquipDefectEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnStockEquipDefectEntity::getIsDel, "0");
        queryWrapper.notIn(EnStockEquipDefectEntity::getDefectStatusCode, "1", "3");
        queryWrapper.selectCount(EnStockEquipDefectEntity::getId, "num");
        queryWrapper.selectAs(EnStockEquipDefectEntity::getDefectStatusCode, "statusCode");
        queryWrapper.selectAs(EnStockEquipDefectEntity::getEquipDefectType, "type");
        queryWrapper.groupBy(EnStockEquipDefectEntity::getDefectStatusCode, EnStockEquipDefectEntity::getEquipDefectType);
        try {
            Date startDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdate);
            Date endDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(edate);
            if (startDateTime != null && !"".equals(startDateTime)) {
                queryWrapper.ge(EnStockEquipDefectEntity::getCreatTime, startDateTime);
            }
            if (endDateTime != null && !"".equals(endDateTime)) {
                queryWrapper.le(EnStockEquipDefectEntity::getCreatTime, endDateTime);
            }
            List<EnDefectsTjVo> entities = baseMapper.selectJoinList(EnDefectsTjVo.class, queryWrapper);
            return entities;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }
}
